package cn.wolfcode.wolf2world.web.controller;

import cn.wolfcode.wolf2world.domain.*;
import cn.wolfcode.wolf2world.query.TravelQueryObject;
import cn.wolfcode.wolf2world.service.DestinationService;
import cn.wolfcode.wolf2world.service.TravelCommentService;
import cn.wolfcode.wolf2world.service.TravelService;
import cn.wolfcode.wolf2world.service.UserInfoService;
import cn.wolfcode.wolf2world.util.UMEditorUploader;
import cn.wolfcode.wolf2world.vo.JsonResult;
import cn.wolfcode.wolf2world.web.annotation.RequiredLogin;
import cn.wolfcode.wolf2world.web.annotation.UserParam;
import jdk.nashorn.internal.objects.annotations.Getter;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

/**
 * @author 杨兴健
 * @Date 2020-08-12 22:51
 */
@RestController
@RequestMapping("/travels")
public class TravelController {
    @Autowired
    private TravelService travelService;
    @Autowired
    private DestinationService destinationService;
    @Autowired
    private TravelCommentService travelCommentService;
    @Autowired
    private UserInfoService userInfoService;


    /**
     * @return
     */
    @RequestMapping("/query")
    public Object query(TravelQueryObject qo) {
        Page<Travel> page = travelService.query(qo);
        return JsonResult.success(page);
    }

    @RequestMapping("/input")
    public Object input() {
        List<Destination> list = destinationService.list();
        return JsonResult.success(list);
    }

    @RequestMapping("/travel")
    public Object travel(String id) {
        Travel travel = travelService.get(id);
        return JsonResult.success(travel);
    }

    /**
     * 添加游记评论
     * @param userInfo
     * @param comment
     * @return
     */
    @RequiredLogin
    @PostMapping("/commentAdd")
    public Object commentAdd(@UserParam UserInfo userInfo, TravelComment comment) {
        //复制userinfo相同属性到comment中
        BeanUtils.copyProperties(userInfo,comment);
        //设置没有复制到的用户id
        comment.setUserId(userInfo.getId());
        //创建时间
        comment.setCreateTime(new Date());
        if (comment.getRefComment() != null && StringUtils.hasLength(comment.getRefComment().getId())) {
            //查询关联的游记评论
            comment.setRefComment(travelCommentService.get(comment.getRefComment().getId()));
            //设置评论类型
            comment.setType(TravelComment.TRAVLE_COMMENT_TYPE);
        }
        //保存
        travelCommentService.save(comment);
        return JsonResult.success();
    }

    @GetMapping("/comments")
    public Object commentAdd(String travelId) {
        List<TravelComment> list = travelCommentService.queryByTravelId(travelId);
        return JsonResult.success(list);
    }

    /**
     * 游记明细
     * @param id
     * @return
     */
    @RequestMapping("/detail")
    public Object detail(String id) {
        Travel travel = travelService.get(id);
        travel.setAuthor(userInfoService.get(travel.getUserId()));
        return JsonResult.success(travel);
    }


    @RequestMapping("/saveOrUpdate")
    public Object saveOrUpdate(Travel travel, @UserParam UserInfo userInfo) {
        travel.setUserId(userInfo.getId());
        travelService.saveOrUpdate(travel);
        return JsonResult.success(travel.getId());
    }


    //上传图片
    @RequestMapping("/contentImage")
    public String uploadUEImage(MultipartFile upfile, HttpServletRequest request) throws Exception{
        UMEditorUploader up = new UMEditorUploader(request);
        String[] fileType = {".gif" , ".png" , ".jpg" , ".jpeg" , ".bmp"};
        up.setAllowFiles(fileType);
        up.setMaxSize(10000); //单位KB
        up.upload(upfile);

        String callback = request.getParameter("callback");
        String result = "{\"name\":\""+ up.getFileName() +"\", \"originalName\": \""+ up.getOriginalName() +"\", \"size\": "+ up.getSize()
                +", \"state\": \""+ up.getState() +"\", \"type\": \""+ up.getType() +"\", \"url\": \""+ up.getUrl() +"\"}";
        result = result.replaceAll( "\\\\", "\\\\" );
        if(callback == null ){
            return result ;
        }else{
            return "<script>"+ callback +"(" + result + ")</script>";
        }
    }



}
